const gulp = require("gulp");
const cssmin = require("gulp-cssmin");
const auto = require("gulp-autoprefixer");
const uglify = require("gulp-uglify");
const babel = require("gulp-babel");
const htmlmin = require("gulp-htmlmin");
const wed = require("gulp-webserver");
const sass = require("gulp-sass-china");

// static
function copyfn(){
    return gulp.src("./project/img/**/*")
            .pipe(gulp.dest("./server/static"))
}
exports.copy = copyfn;


// css 压缩
function cssfn(){
    return gulp.src("./project/css/**/*")
            .pipe(auto("last 2 version","safari 5","ie 8","ie 9","opera 12.1","ios 6","android 4"))
            .pipe(cssmin())
            .pipe(gulp.dest("./server/css"))
}
exports.css = cssfn;

// sass
function sassfn(){
    return gulp.src("./project/sass/**/*")
    .pipe(sass())
    // 压缩添加前缀
    // .pipe(autoprefixer("last 2 version","safari 5","ie 8","ie 9","opera 12.1","ios 6","android 4"))
    // .pipe(cssmin())
    .pipe(gulp.dest("./server/sass"))
}
exports.sass = sassfn;

// js
function jsfn(){
    return gulp.src("./project/js/**/*")
            .pipe(babel({
                presets:["@babel/env"]
            }))
            .pipe(uglify())
            .pipe(gulp.dest("./server/js"))
}
exports.js = jsfn;

// html
function htmlfn(){
    return gulp.src("./project/pages/**/*")
            .pipe(htmlmin({
                removeEmptyAttributes:true,
                collapseWhitespace:true
            }))
            .pipe(gulp.dest("./server/pages"))
}
exports.html = htmlfn;

// 首页
function indexfn(){
    return gulp.src("./project/index.html")
            .pipe(gulp.dest("./server/"))
}
exports.index = indexfn;

function cartfn(){
    return gulp.src("./project/cart.html")
            .pipe(gulp.dest("./server/"))
}
exports.index = cartfn;

function detailsfn(){
    return gulp.src("./project/details.html")
            .pipe(gulp.dest("./server/"))
}
exports.index = detailsfn;

function listfn(){
    return gulp.src("./project/list.html")
            .pipe(gulp.dest("./server/"))
}
exports.index = listfn;

function logonfn(){
    return gulp.src("./project/logon.html")
            .pipe(gulp.dest("./server/"))
}
exports.index = logonfn;

function regstfn(){
    return gulp.src("./project/regst.html")
            .pipe(gulp.dest("./server/"))
}
exports.index = regstfn;
// 定义服务器
function serverfn(){
    return gulp.src("./server")
            .pipe(wed({
                host:"localhost",
                port:"3000",
                livereload:true,
                open:"./index.html",
                proxies:[{
                    source:"/asd",
                    target:"https://wanandroid.com/wxarticle/chapters/json"
                }]
            }))
}

exports.server = serverfn;


// 监听
function watchallfn(){
    gulp.watch("./project/index.html",indexfn);
    gulp.watch("./project/pages/**/*",htmlfn);
    gulp.watch("./project/js/**/*",jsfn);
    gulp.watch("./project/sass/**/*",sassfn);
    gulp.watch("./project/css/**/*",cssfn);
    gulp.watch("./project/static/**/*",copyfn);
}
exports.watchall = watchallfn;


exports.all = gulp.series(

    gulp.parallel(indexfn,htmlfn,jsfn,cssfn,sassfn,copyfn),

    gulp.parallel(watchallfn,serverfn)
);